<?php

namespace app\controller;

use app\BaseController;
use app\model\Prize;
use app\model\Odds;
use app\model\AgeOdds;
use think\exception\ValidateException;

class Index extends BaseController
{
    public function index()
    {
        echo 1111;
    }

    // 添加奖品
    public function addprize(Prize $prize)
    {
        $name = $this->request->post('name');
        $odds = $this->request->post('odds');
        $count = $this->request->post('count');
        $file = request()->file('image');

        try {
            $this->validate([
                'name' => $name,
                'odds' => $odds,
                'file' => $file,
                'count' => $count,
            ], [
                'name' => 'require',
                'odds' => 'require',
                'file' => 'require',
                'count' => 'require',
            ], [
                'name.require' => '奖品名不能为空',
                'odds.require' => '概率不能为空',
                'file.require' => '图片不能为空',
                'count.require' => '数量不能为空',
            ]);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }

        $savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);

        $prize->add($name, '/storage/' . $savename, $odds, $count);

        return $this->success('操作成功');
    }

    // 修改奖品
    public function updprize(Prize $prize)
    {
        $data = $this->request->post();

        $file = request()->file('image');
        if (!empty($file)) {
            $savename = \think\facade\Filesystem::disk('public')->putFile('topic', $file);
            $data['icon'] = '/storage/' . $savename;
        }

        $prize->upd($data);

        return $this->success('操作成功');
    }

    // 删除奖品
    public function delprize(Prize $prize)
    {
        $id = $this->request->post('id');
        $prize->del($id);
        return $this->success('操作成功');
    }

    // 设置男生概率
    public function setman(Odds $oddsModel)
    {
        $odds = $this->request->post('odds');
        $oddsModel->setman($odds);
        return $this->success('操作成功');
    }

    // 设置女生概率
    public function setwoman(Odds $oddsModel)
    {
        $odds = $this->request->post('odds');
        $oddsModel->setwoman($odds);
        return $this->success('操作成功');
    }

    // 新增年龄概率
    public function setAge(AgeOdds $ageModel)
    {
        $data = $this->request->post();

        try {
            $this->validate($data, [
                'max' => 'require',
                'min' => 'require',
                'odds' => 'require',
            ], [
                'max.require' => '年龄最大值不能为空',
                'min.require' => '年龄最小值不能为空',
                'odds.require' => '年龄几率不能为空',
            ]);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }

        if ($data['max'] < $data['min']) {
            return $this->error('最大值不能少于最小值');
        }

        $ageModel->add($data['max'], $data['min'], $data['odds']);
        return $this->success('操作成功');
    }

    // 修改年龄
    public function updAge(AgeOdds $ageModel)
    {
        $data = $this->request->post();
        if ($data['max'] < $data['min']) {
            return $this->error('最大值不能少于最小值');
        }
        $ageModel->upd($data);
        return $this->success('操作成功');
    }

    // 删除年龄概率
    public function delAge(AgeOdds $ageModel)
    {
        $id = $this->request->post('id');
        $ageModel->del($id);
        return $this->success('操作成功');
    }

    // 抽奖
    public function choujiang()
    {
        $o1 = mt_rand(1, 100);
        $o2 = mt_rand(1, 100);
        $o3 = mt_rand(1, 100);
        $o4 = mt_rand(1, 100);

        $age = 18;
        $sex = 1;

        $man = Odds::where('key', 'man')->value('odds');
        $woman = Odds::where('key', 'woman')->value('odds');

        // 抽男生
        if ($sex == 1 && $man < $o1) {
            return $this->error('没抽中');
        }
        // 抽女生
        if ($sex == 0 && $woman < $o2) {
            return $this->error('没抽中');
        }

        $ageData = AgeOdds::select();
        $ageOdds = 0;
        foreach ($ageData as $item) {
            if ($age < $item->max && $age >= $item->min) {
                $ageOdds = $item->odds;
                break;
            }
        }

        // 抽年龄
        if ($ageOdds < $o3) {
            return $this->error('没抽中');
        }

        // 抽奖品
        $prize = Prize::select();
        $oddsCount = 0;
        $zhong = null;
        foreach ($prize as $item) {
            if ($oddsCount <= $o4 && (($oddsCount + $item->odds) >= $o4)) {
                $zhong = $item->toArray();
            }
            $oddsCount += $item->odds;
        }
        if ($oddsCount > 100) $oddsCount = 100;
        if ($oddsCount < $o4 || empty($zhong)) {
            return $this->error('没抽中');
        }

        return $this->success('中了');
    }
}
